﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaDefinitionRepositoryTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MediaDefinitionRepositoryTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EFRepositoryTests
{
  using System;
  using System.Configuration;
  using System.Data.Entity;
  using System.Linq;
  using System.Transactions;
  using BuildMeA;
  using DocProcessingRepository.Repositories;
  using Entities;
  using FluentAssertions;
  using NUnit.Framework;

  [Category("Integration")]
  [TestFixture]
  public class MediaDefinitionRepositoryTests
  {
    private TransactionScope transactionScope;
    private MediaDefinitionRepository mediaDefinitionRepository;
    private ApplicationUserRepository applicationUserRepository;

    private ApplicationUser user;
    private MediaDefinition mediaDefinition;

    [SetUp]
    public void Setup()
    {
      this.transactionScope = new TransactionScope();

      String connectionString = ConfigurationManager.ConnectionStrings["BBOS"].ConnectionString;

      this.mediaDefinitionRepository = new MediaDefinitionRepository(connectionString);
      this.applicationUserRepository = new ApplicationUserRepository(connectionString);

      this.user = BuildMeA.ApplicationUser("user1");
      this.applicationUserRepository.Create(this.user);

      this.mediaDefinition = BuildMeA.MediaDefinition(1, 1, 1, 1, this.user.Id, "test", false, false);
    }

    [TearDown]
    public void TearDown()
    {
      this.transactionScope.Dispose();
    }

    [Test]
    public void GivenAUser_WhenIAskForTheManCosTheyHaveAccessTo_IGetManCos()
    {
      this.mediaDefinitionRepository.Create(this.mediaDefinition);

      PagedResult<MediaDefinitionEntity> result = this.mediaDefinitionRepository.GetByManCoDoc(1, 3, 1);

      result.TotalItems.Should().BeGreaterThan(0);
    }

    [Test]
    public void GivenValidData_WhenIUpdateThePrintFlagToTrue_TheDatabaseIsUpdated()
    {
      this.mediaDefinitionRepository.Create(this.mediaDefinition);

      MediaDefinition existingMediaDefinition =
        this.mediaDefinitionRepository.Entities
        .Single(md => md.MediaDefID == this.mediaDefinition.MediaDefID);

      existingMediaDefinition.Print.Should().Be(false);

      this.mediaDefinitionRepository.UpdatePrintFlag(this.mediaDefinition.MediaDefID, true);

      existingMediaDefinition =
        this.mediaDefinitionRepository.Entities
        .AsNoTracking()
        .Single(md => md.MediaDefID == this.mediaDefinition.MediaDefID);

      existingMediaDefinition.Print.Should().Be(true);
    }

    [Test]
    public void GivenValidData_WhenIUpdateThePrintFlagToFalse_TheDatabaseIsUpdated()
    {
      this.mediaDefinition.Print = true;
      this.mediaDefinitionRepository.Create(this.mediaDefinition);

      MediaDefinition existingMediaDefinition =
        this.mediaDefinitionRepository.Entities
        .Single(md => md.MediaDefID == this.mediaDefinition.MediaDefID);

      existingMediaDefinition.Print.Should().Be(true);

      this.mediaDefinitionRepository.UpdatePrintFlag(this.mediaDefinition.MediaDefID, false);

      existingMediaDefinition =
        this.mediaDefinitionRepository.Entities
        .AsNoTracking()
        .Single(md => md.MediaDefID == this.mediaDefinition.MediaDefID);

      existingMediaDefinition.Print.Should().Be(false);
    }
  }
}